<?php
    require("../wp-blog-header.php");
    require("event-config.php");
    
    if(get_user_role() != "administrator")
    {
        header("Location: error.php");
    }
?>

<html>
<head>
    <title>Trang quản lí sự kiện</title>
    <meta charset='utf-8' />
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/event.js"></script>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
    <header>
        <div class="header-wrapper">
            <img src="img/logo.png" class="header-logo" />
            <p class="header-title current">Quản lí sự kiện</p>
            <p class="header-title"><a class="header-link" href="create-event.php">Tạo sự kiện</a></p>
        </div>
    </header>
    
    <div class='container'>
        <div class='post-wrapper'>
            <h4>Danh sách sự kiện</h4>
            <?php 
                if(isset($_GET['page']))
                {
                    $from = $_GET['page'] * $NUM_OF_POST;
                    $next = $_GET['page'] + 1;
                }
                else
                {
                    $from = 0;
                    $next = 1;
                }
                $query = "SELECT `event_id`,`name`, 
                DATE_FORMAT(`start_date`,'%d-%m-%Y') AS `start_date`, 
                DATE_FORMAT(`end_date`,'%d-%m-%Y') AS `end_date`,
                `benefit`, `description`, `slot` 
                FROM $EVENT ORDER BY `event_id` DESC LIMIT $from, $NUM_OF_POST";

                $result = execute($query);
                while($row = next_row($result))
                {
                    $event_id = $row['event_id'];
                    $name = $row['name'];
                    $link_post = $LINK . "event.php?p=" . $event_id;
                    echo "<div class='post' id='event-$event_id'>
                            <a href='event.php?p=$event_id' target='_blank'><p class='post-title'>$name</p></a>
                            <div class='post-content'>
                                <div class='post-info'>
                                    Link: 
                                        <input type='text' class='post-link' value='$link_post' disabled />
                                </div>
                                <div class='post-action'>
                                    <a href='list-attendee.php?p=$event_id' target='_blank'><button class='btn bt-list'>Danh sách tham gia</button></a>
                                    <a href='edit-event.php?p=$event_id' target='_blank'><button class='btn bt-edit'>Chỉnh sửa</button></a>
                                    <button class='btn bt-delete' data-id='$event_id'>Xoá</button>
                                    <img class='loading' id='loading-$event_id' src='img/loading.gif' />
                                </div>
                            </div>
                            <p class='post-separate'></p>
                          </div>";
                }
                $limit = $next * $NUM_OF_POST;
                if ($limit < event_count())
                {
                    $link_next = $LINK . "?page=" . $next;
                    echo "<button class='btn btn-6px' onclick='window.location.href = $link_next'>Xem tiếp</button>";
                }
                
             ?>
        </div>
    </div>
</body>
</html>